package com.testng.oa.testcase;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.testng.oa.pub.oaindex;
import com.testng.oa.pub.oalogin;

import static org.testng.Assert.assertEquals;
public class oalogintest {
	private static WebDriver driver = null;
	private String baseUrl;
	private String handle;

	@BeforeClass
	public void setUp() throws Exception {
		
		driver = new ChromeDriver();
		baseUrl = "http://localhost/oa/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.get(baseUrl);
		handle = driver.getWindowHandle();
	}
	
	@AfterClass
	public void tearDown() throws Exception {
		driver.quit();
	}
	
	@DataProvider(name="user")
	public Object[][] Users(){
		return new Object[][]{
			{"admin","123456",true},
			{"","",true},
			{"admin","123456",false},
		};
	}
	
	
	@Test(dataProvider="user")
	public void testoalogin(String username, String password, Boolean rempass) {
		if(rempass) {
			WebElement element1 = oalogin.login_rempass(driver);
			if (!element1.isSelected()) {
				element1.click();
				oalogin.oa_login(driver,username,password);
			}else {
				oalogin.login_Button(driver).click();
			}
		}else {
			WebElement element1 = oalogin.login_rempass(driver);
			if (element1.isSelected()) {
				element1.click();
			}
			oalogin.oa_login(driver, username, password);
		}
		oaindex.getlink(driver, "退出").click();
	}
	
	@DataProvider(name="user1")
	public Object[][] Userswrong(){
		return new Object[][]{
			{"admin","","密码不能为空"},
			{"","123456","帐号不能为空"},
			{"","","帐号不能为空"},
			{"admin","123","密码不对"},
			{"admin1","12345","用户不存在"},
		};
	}
	
	@Test(dataProvider="user1")
	public void testoaloginwithwrong(String username, String password, String wrongstr) throws InterruptedException {
		oalogin.oa_login(driver, username, password);
		Thread.sleep(500);
		String wstr = oalogin.login_GetWrongStr(driver).getText();
		assertEquals(wstr,wrongstr);
	}
	@DataProvider(name="link")
	public Object[][] links(){
		return new Object[][]{
			{"关于我们","http://www.rockoa.com/about.html"},
			{"软件介绍","http://www.rockoa.com/view_rockoa.html"},
			{"进入官网","http://www.rockoa.com/"},
			{"微信办公","http://www.rockoa.com/view_weixin.html"},
			{"RockOA开发团队","http://www.rockoa.com/"},
		};
	}
	@Test(dataProvider="link")
	public void testlink(String linkText, String link) {
		driver.switchTo().window(handle);
		String url = oalogin.openLink(driver, linkText, handle);
		assertEquals(link,url);
	}
	
	@Test
	public void testrempass() {
		oalogin.login_rempass(driver).click();
		
	}
}
